<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px gw-flex-between"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="指标名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="resetQuery" plain type="primary">重置</el-button>
        <el-button @click="handleQuery" type="primary">查询</el-button>
        <div class="btn-ml">
          <el-button
            type="primary"
            @click="openForm('create')"
            v-hasPermi="['multiModel:indicator:create']"
          >
            新增评测指标
          </el-button>
        </div>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap style="margin-bottom: 0px;">
    <el-table
      :data="list"
      :stripe="true"
      :show-overflow-tooltip="true"
      :header-cell-style="{ 'background': '#CCEBE7', 'color': '#585858' }"
      :align="'center'"
      :max-height="tableMaxHeight"
      >
      <el-table-column label="序号" type="index" width="60px" align="center" />
      <el-table-column label="指标编码" align="center" prop="code" />
      <el-table-column label="指标名称" align="center" prop="name" />
      <el-table-column label="描述" align="center" prop="remark" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" width="180px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('view', scope.row.id)"
            v-hasPermi="['multiModel:indicator:query']"
          >
            查看
          </el-button>
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['multiModel:indicator:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['multiModel:indicator:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <IndicatorForm ref="formRef" @success="getList" />
  <DelConfirmDialog ref="delConfirmRef" @confirm="deleteConfirm" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import { IndicatorApi, IndicatorVO } from '@/api/multiModel/indicator'
import IndicatorForm from './IndicatorForm.vue'

/** 指标 列表 */
defineOptions({ name: 'Indicator' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const list = ref<IndicatorVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: undefined,
})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  try {
    const data = await IndicatorApi.getIndicatorPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const delConfirmRef = ref()
const handleDelete = async (id: number) => {
  delConfirmRef.value.open(id, ['是否确定删除选中指标?', '删除后指标将无法进行调用测评!'])
}
/** 删除按钮操作 */
const deleteConfirm = async (id: number) => {
  try {
    await IndicatorApi.deleteIndicator(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}
const tableMaxHeight = ref('300px'); // 初始高度
// 表格高度
const updateMaxHeight = () => {
  tableMaxHeight.value = document.body.clientHeight - 380 + 'px'
}
window.addEventListener('resize', updateMaxHeight)
/** 初始化 **/
onMounted(() => {
  getList()
  updateMaxHeight()
})
onUnmounted(() => {
  window.removeEventListener('resize', updateMaxHeight)
})
</script>